<?php
/**
 * 抢购活动管理
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Log;
class PTaskController extends Controller
{
    protected $model;
    protected $return;
   
    public function __construct(){
        header("Access-Control-Allow-Origin: *");
    }

    //每月初抽奖自动恢复库存
    public function run()
    {
        $day = date('d');
        if ($day != '01') {
            return false;
        }
        //已执行
        S(array('type'=>'redis'));
        $key = '__DO_TASK__';
        if(S($key)){
            Log::write("本月已执行");
            return false;
        }
        S(array('type'=>'redis','expire'=>24 * 60 * 60));
        S($key, true);
        $map['status'] = 1;
        $map['is_deleted'] = 0;
        $map['start_time'] = array('elt', time());
        $map['end_time'] = array('egt', time());
        $lotterys = M("PLottery")->where($map)->select();
        if (!$lotterys) {
            return false;
        }
        foreach ($lotterys as $v) {
            $prizeMap['is_deleted'] = 0;
            $prizeMap['lottery_id'] = $v['id'];
            $data['surplus'] = array('exp', 'sku');
            M("PLotteryPrize")->where($prizeMap)->save($data);
        }
    }    

}